const { Pool } = require('pg');
const config = require('../config');

const pool = new Pool(config.db);

// 验证数据库连接的函数
const testConnection = async () => {
  try {
    const client = await pool.connect();
    const result = await client.query('SELECT NOW()');
    client.release();
    console.log('数据库连接成功！服务器时间:', result.rows[0].now);
    return { success: true, time: result.rows[0].now };
  } catch (err) {
    console.error('数据库连接失败:', err.message);
    return { success: false, error: err.message };
  }
};

module.exports = {
  query: (text, params) => pool.query(text, params),
  pool, // Expose pool for transactions if needed
  testConnection // 导出测试连接函数
};